Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert jQuery data attribute to number #3899

Merged
merged 1 commit into from
Jan 28, 2021

Conversation

spaghetticode
Copy link
Member

@spaghetticode spaghetticode commented Jan 19, 2021

JQuery numeric string data attributes are not casted to numbers consistently.

There are instances when they are kept as string, this behavior seems to be related to the presence of insignificant
zeros in the original string:

  $('<div data-price="12.00"></div>').data('price')
  > "12.00"

  $('<div data-price="12.10"></div>').data('price')
  > "12.10"

but:

  $('<div data-price="12.1"></div>').data('price')
  > 12.1

So, we need to enforce the typecast when summing those attributes in the function totalSelectedReturnItemAmount.

After the sum, since we want to show a 2 decimal number, we need to convert the total amount with toFixed(2).

Before After
Screen Shot 2021-01-19 at 9 40 02 AM Screen Shot 2021-01-19 at 9 45 02 AM

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@spaghetticode spaghetticode added the type:bug Error, flaw or fault label Jan 19, 2021
@spaghetticode spaghetticode self-assigned this Jan 19, 2021
@kennyadsl
Copy link
Member

@spaghetticode nice finding. Did you have the chance to understand if this has different implications with other locales?

@spaghetticode
Copy link
Member Author

spaghetticode commented Jan 19, 2021

@kennyadsl I did not. I'm assuming everyone is converting to 2 decimals as of today, if that's what concerns you about locales. It would not be that hard to change it to use the digits that are found in the data attribute numbers though.

Also, I was able to find the reason for this strange behavior, see this line: https://github.com/jquery/jquery/blob/a32cf6324f8f2190e66a687e94be9687ebf840b7/src/data.js#L34

"12.01" === +"12.01" + ""
> true

"12.00" === +"12.00" + ""
> false

@aldesantis
Copy link
Member

🤦

Thanks for the fix @spaghetticode!

As for the precision, I'm pretty sure there are currencies that use a different numbers of decimals (e.g., the Japanese Yen comes to mind — as far as I know, they use no decimals at all). Perhaps we can get this information from the Money gem?

JQuery numeric string data attributes are not casted to numbers
consistently. There are instances when they are kept as string,
this behavior seems to be related to the presence of insignificant
zeros in the original string:

  $('<div data-price="12.00"></div>').data('price')
  > "12.00"

  $('<div data-price="12.10"></div>').data('price')
  > "12.10"

but:

  $('<div data-price="12.1"></div>').data('price')
  > 12.1

So, we need to enforce the typecast to number when summing those
attributes in the function `totalSelectedReturnItemAmount`.

After the sum, the number needs to be converted to a properly
localized amount. For this we leverage the existing function
`formatNumber` from the library `accounting.js`, see:

https://github.com/solidusio/solidus/blob/master/backend/vendor/assets/javascripts/solidus_admin/accounting.js
@spaghetticode spaghetticode force-pushed the spaghetticode/fix-total-amount branch 2 times, most recently from a486ed6 to 1f6b44e Compare January 19, 2021 13:42
@spaghetticode
Copy link
Member Author

@aldesantis thanks for the input, it now works properly also for those countries that don't have decimal amounts, and the number is properly localized too. Unfortunately, we don't have access to the currency here, and adding it would make things a bit too complicated, still can leverage some locale information such as the currency delimiter and separator that IMHO will suffice.

Copy link
Member

@aldesantis aldesantis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spaghetticode awesome, thanks!

@aldesantis aldesantis requested a review from a team January 20, 2021 17:53
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kennyadsl kennyadsl added this to the 2.11 milestone Jan 21, 2021
@spaghetticode spaghetticode added the changelog:solidus_backend Changes to the solidus_backend gem label Jan 21, 2021
@kennyadsl kennyadsl merged commit 5bad6c6 into solidusio:master Jan 28, 2021
@kennyadsl kennyadsl deleted the spaghetticode/fix-total-amount branch January 28, 2021 15:16
@kennyadsl
Copy link
Member

Failing spec is already addressed in master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_backend Changes to the solidus_backend gem type:bug Error, flaw or fault
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants